package com.liaoyi.myble;

import android.app.Application;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import com.liaoyi.myble.service.BluetoothService;

public class App extends Application {
    public static App instance;
    private static final String TAG = "App";
    public BluetoothService bluetoothService;
    private ServiceConnection connection = new ServiceConnection(){

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.e(TAG, "onServiceConnected: 服务绑定成功" );
            bluetoothService = ((BluetoothService.BluetoothBinder) iBinder).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.e(TAG, "onServiceDisconnected: 服务销毁成功" );
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        Intent bluetootIntent = new Intent(this, BluetoothService.class);
        bindService(bluetootIntent,connection, BIND_AUTO_CREATE);
    }

    /**
     * 解绑服务
     */
    public void unbindService(){
        unbindService(connection);
    }
}
